<?php

namespace app\api\service\alipay;

use think\Db;
use think\Exception;
use think\Loader;
use think\Log;

Loader::import('Alipay_APP.aop.AopClient');
Loader::import('Alipay_APP.aop.request.AlipayTradeAppPayRequest');

/**
 * 支付回调
 */
class BaseNotify
{

    // 回调
    public function alipayNotify(){
        $data = $_POST;
        $aop = new \AopClient;
        $aop->alipayrsaPublicKey = config('alipay.alipay_public_key');
        $flag = $aop->rsaCheckV1($data, NULL, "RSA2");
        //$flag = $aop->rsaCheckV1($data, config('alipay.alipay_public_key'), "RSA2");

        $data['flag'] = $flag;
        return $data;
    }

    // 数组转XML
    public function arrayToXml($arr){
        $xml = "<xml>";
        foreach ($arr as $key=>$val) {
            if (is_numeric($val)) {
                $xml.="<".$key.">".$val."</".$key.">";
            } else {
                $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
            }
        }
        $xml.="</xml>";

        return $xml;
    }

    // 将XML转为array
    public function xmlToArray($xml){
        // 禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $values = json_decode(json_encode(simplexml_load_string($xml,'SimpleXMLElement', LIBXML_NOCDATA)), true);

        return $values;
    }
}